package com.sq.universe.base.conf;

import com.sq.universe.base.constants.BaseConstants;
import com.sq.universe.base.utils.AuthUtils;
import com.sq.universe.system.entity.UserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CustomSessionManager extends DefaultWebSessionManager {

    private static final String AUTHORIZATION = "X-Token";

    private static final String REFERENCED_SESSION_ID_SOURCE = "Stateless request";

    @Autowired
    private RedisSessionDAO redisSessionDAO;

    @Value("${shiro.annUrl:}")
    private String annUrl;

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        String id = WebUtils.toHttp(request).getHeader(AUTHORIZATION);
        if(id == null){
            id = request.getParameter("X-Token");
        }
        //如果请求头中有 Authorization 则其值为sessionId
        if (!StringUtils.isEmpty(id)) {
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
            //否则按默认规则从cookie取sessionId
            redisSessionDAO.update(redisSessionDAO.readSession(id));
            return id;
        } else {
            Boolean flag = false;
            String url = WebUtils.toHttp(request).getRequestURI();
            if("/system/user/login".equals(url)){
                flag = true;
            }
            AntPathMatcher matcher = new AntPathMatcher();
            String[] annUrlArr = annUrl.split(",");
            List<String> annurlList = new ArrayList<>();
            for (int i = 0; i < annUrlArr.length; i++) {
                annurlList.add(annUrlArr[i]);
            }
            annurlList.add("/ureport/ureport/**");
            annurlList.add("/ureport/ureport/**/**");
            annurlList.add("/ureport/ureport/**/**/**");
            annurlList.add("/ureport/ureport/**/**/**/**");
            annurlList.add("/flowable/static/**");
            annurlList.add("/flowable/static/**");
            annurlList.add("/flowable/");
            annurlList.add("/flowable/libs/**");
            annurlList.add("/flowable/fonts/**");
            annurlList.add("/flowable/styles/**");
            annurlList.add("/flowable/scripts/**");
            annurlList.add("/flowable/editor-app/**");
            annurlList.add("/flowable/*.ico");
            annurlList.add("/flowable/*.json");
            annurlList.add("/flowable/images/**");
            annurlList.add("/flowable/display/**");
            annurlList.add("/flowable/i18n/**");
            annurlList.add("/flowable/views/**");
            annurlList.add("/flowable/modeler-app/rest/models/*/*");
            for (int i = 0; i < annurlList.size(); i++) {
                if(matcher.match(annurlList.get(i),url)){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                AuthUtils.getCurrentUser();
            }
            Serializable newId = super.getSessionId(request, response);
            return newId;
        }
    }

}
